package com.hf.wujian;

import com.google.gson.Gson;
import com.hf.wujian.constant.Command;
import com.hf.wujian.netty.BSHClientInitializer;
import com.hf.wujian.netty.NettyMsg;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.*;

public class Client3 {

    public static void main(String[] args) {
        new Client3.ClientThread().start();
    }

    static class ClientThread extends Thread{

        private String host = "127.0.0.1";
        private int port = 10086;
        private String  uuid = UUID.randomUUID().toString();

        @Override
        public void run() {
            super.run();

            Random rand = new Random();
            int loginTime = rand.nextInt(3);
            int sleepTime = rand.nextInt(3)+3;

            System.out.println("线程" + getName() + "已经启动,启动延迟为" + loginTime + "秒,睡眠时间为" + sleepTime + "秒");
            EventLoopGroup group = new NioEventLoopGroup();
            Bootstrap bootstrap = new Bootstrap();
            try {
                bootstrap.group(group)
                        .channel(NioSocketChannel.class)
                        .handler(new BSHClientInitializer());

                // 连接服务端
                Channel channel = bootstrap.connect(host, port).sync().channel();

                login(channel,(rand.nextInt(10000)+10000) + "");

                Scanner scanner = new Scanner(System.in);
                while (true) {
                    String str = scanner.nextLine();
                    if("exit".equals(str)){
                        break;
                    }

                    switch (str){
                        case "getrooms":
                            getRooms(channel);
                            break;
                        case "join":
                            System.out.println("请输入房间名：");
                            String roomName = scanner.nextLine();
                            joinRoom1(roomName,channel);
                            break;
                        case "cancel":
                            cancelRoom(channel);
                            break;
                        case "create":
                            System.out.println("请输入房间名：");
                            String createName = scanner.nextLine();
                            createRoom1(channel,createName);
                            break;
                        case "getnames":
                            getNames(channel);
                            break;
                        case "prepare":
                            prepare(channel);
                            break;
                    }
                }

                Thread.sleep(sleepTime * 1000);
                exit(channel);

                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                group.shutdownGracefully();
            }
        }

        private void prepare(Channel channel){
            NettyMsg nettyMsg = new NettyMsg();
            nettyMsg.setUuid(uuid);
            nettyMsg.setCommand(Command.COMMAND_PREPARE);
            channel.writeAndFlush(new Gson().toJson(nettyMsg) + "\r\n");
        }

        private void getNames(Channel channel){
            NettyMsg nettyMsg = new NettyMsg();
            nettyMsg.setUuid(uuid);
            nettyMsg.setCommand(Command.COMMAND_GET_ROOM_USER_INFO);
            channel.writeAndFlush(new Gson().toJson(nettyMsg) + "\r\n");
        }

        private void login(Channel channel,String name){
            Map<String,Object> data = new HashMap<>();
            NettyMsg nettyMsg = new NettyMsg();
            nettyMsg.setUuid(uuid);
            nettyMsg.setCommand(Command.COMMAND_LOGIN);
            data.put("userName",name);
            nettyMsg.setData(data);
            channel.writeAndFlush(new Gson().toJson(nettyMsg) + "\r\n");
        }

        private void exit(Channel channel){
            NettyMsg nettyMsg = new NettyMsg();
            nettyMsg.setUuid(uuid);
            nettyMsg.setCommand(Command.COMMAND_EXIT);
            channel.writeAndFlush(new Gson().toJson(nettyMsg) + "\r\n");
        }

        private void createRoom(Channel channel,int num){
            Map<String,Object> data = new HashMap<>();
            NettyMsg nettyMsg = new NettyMsg();
            nettyMsg.setUuid(uuid);
            nettyMsg.setCommand(Command.COMMAND_CREATEGAMEROOM);
            data.put("userName","用户名");
            nettyMsg.setData(data);
            data.put("roomName",num + "");
            channel.writeAndFlush(new Gson().toJson(nettyMsg) + "\r\n");
        }

        private void createRoom1(Channel channel,String roomName){
            Map<String,Object> data = new HashMap<>();
            NettyMsg nettyMsg = new NettyMsg();
            nettyMsg.setUuid(uuid);
            nettyMsg.setCommand(Command.COMMAND_CREATEGAMEROOM);
            data.put("userName","用户名");
            nettyMsg.setData(data);
            data.put("roomName",roomName + "");
            channel.writeAndFlush(new Gson().toJson(nettyMsg) + "\r\n");
        }

        private void distoryRoom(Channel channel,int num){
            Map<String,Object> data = new HashMap<>();
            NettyMsg nettyMsg = new NettyMsg();
            nettyMsg.setUuid(uuid);
            nettyMsg.setCommand(Command.COMMAND_DESTORYGAMEROOM);
            data.put("roomName",num + "");
            nettyMsg.setData(data);
            channel.writeAndFlush(new Gson().toJson(nettyMsg) + "\r\n");
        }

        private void joinRoom(Channel channel){
            Map<String,Object> data = new HashMap<>();
            NettyMsg nettyMsg = new NettyMsg();
            nettyMsg.setUuid(uuid);
            nettyMsg.setCommand(Command.COMMAND_JOINGAMEROOM);
            channel.writeAndFlush(new Gson().toJson(nettyMsg) + "\r\n");
        }


        private void joinRoom1(String roomName,Channel channel){
            Map<String,Object> data = new HashMap<>();
            NettyMsg nettyMsg = new NettyMsg();
            nettyMsg.setUuid(uuid);
            nettyMsg.setCommand(Command.COMMAND_JOINGAMEROOM);
            data.put("roomName",roomName);
            nettyMsg.setData(data);
            channel.writeAndFlush(new Gson().toJson(nettyMsg) + "\r\n");
        }

        private void cancelRoom(Channel channel){
            Map<String,Object> data = new HashMap<>();
            NettyMsg nettyMsg = new NettyMsg();
            nettyMsg.setUuid(uuid);
            nettyMsg.setCommand(Command.COMMAND_CANCELGAMEROOM);
            channel.writeAndFlush(new Gson().toJson(nettyMsg) + "\r\n");
        }

        private void getRooms(Channel channel){
            Map<String,Object> data = new HashMap<>();
            NettyMsg nettyMsg = new NettyMsg();
            nettyMsg.setUuid(uuid);
            nettyMsg.setCommand(Command.COMMAND_GET_ROOMS);
            channel.writeAndFlush(new Gson().toJson(nettyMsg) + "\r\n");
        }
    }
}
